package gui.QualityControl;

import gui.engraving.EngravingFrame;
import gui.engraving.EngravingPanel;

import java.awt.ComponentOrientation;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import clients.RPCClient;
import java.awt.SystemColor;

/**
 * @author Idan
 * Quality Control Frame class
 */
public class QCFrame extends JFrame
{
	
	public static RPCClient qcClient;
	protected QCTabbedPane myTabbedPane;
	protected QCListOfOrders myListOfOrders;
	protected QCReportPanel myNewReport;

	
	
	
	 public QCFrame()
	    {
	 	getContentPane().setBackground(SystemColor.inactiveCaption);
	    	setIconImage(Toolkit.getDefaultToolkit().getImage(EngravingFrame.class.getResource("/graphics/Spearhead.png")));
	    	setTitle("ח.נ.י.ת");
	        initComponents();
	        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
	    }

	  
	    @SuppressWarnings("unchecked")
	  
	    private void initComponents()
	    {

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 559, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 481, Short.MAX_VALUE)
	        );

	        pack();
	    }
	    public static void main(String args[])
	    {
	        
	       
	        try {
	            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
	                if ("Nimbus".equals(info.getName())) {
	                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
	                    break;
	                }
	            }
	        } catch (ClassNotFoundException ex) {
	            java.util.logging.Logger.getLogger(EngravingFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (InstantiationException ex) {
	            java.util.logging.Logger.getLogger(EngravingFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (IllegalAccessException ex) {
	            java.util.logging.Logger.getLogger(EngravingFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
	            java.util.logging.Logger.getLogger(EngravingFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	        }

	        
	        /**
	         *  Create client and connect to the Server
	         *  */

	        
	            try
	            {
	               qcClient = new RPCClient();
	               if  (qcClient== null)
	            	   System.exit(0);
	               
	          

	               // get list of orders
	           }
	           catch  (Exception e) 
	           {
	             //e.printStackTrace();
	           }
	            
	        
	        /* Create and display the form */
	            SwingUtilities.invokeLater(new Runnable()
	        {
	            public void run() 
	            {
	                QCFrame qualityControlFrame = new QCFrame();
	                
	                // tabbed pane
	                qualityControlFrame.myTabbedPane = new QCTabbedPane(qualityControlFrame);
	                
	                // list of orders
	                qualityControlFrame.myListOfOrders = new QCListOfOrders(qualityControlFrame);
	                
	                // new report
	                qualityControlFrame.myNewReport = new QCReportPanel(qualityControlFrame);


	                
	                // set up tabbed pane
	                qualityControlFrame.myTabbedPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
	                qualityControlFrame.myTabbedPane.setTabPlacement(JTabbedPane.TOP);
	                
	                ImageIcon listOfOrdersIcon = new ImageIcon(EngravingFrame.class.getResource("/graphics/listoforders.png"));
	                ImageIcon qcReportIcon = new ImageIcon(EngravingFrame.class.getResource("/graphics/qcReport.png"));
	                

	                qualityControlFrame.myTabbedPane.addTab("רשימת פק''עות לדיווח", listOfOrdersIcon, qualityControlFrame.myListOfOrders);
	                qualityControlFrame.myTabbedPane.addTab("דוח בקרת איכות", qcReportIcon,  qualityControlFrame.myNewReport);
	                	                
	                qualityControlFrame.setContentPane(qualityControlFrame.myTabbedPane);
	                
	                // call functions
	                ArrayList<Object> response = initializeAllFields();
	  
	              
	            
	            if (!response.get(0).toString().equalsIgnoreCase("0"))
	            {	
	            	qualityControlFrame.myListOfOrders.setVariables((ArrayList<Object>)response);
	            		            
	                qualityControlFrame.setVisible(true);  
	            }
	            else
	            {
	            	if (response.get(1).toString().equalsIgnoreCase("SQL Failed"))
	            	{
	            		JOptionPane.showMessageDialog(qualityControlFrame,
							     "אראה שגיאה בשרת הנתונים. המערכת תסגר כעת.","שגיאה",JOptionPane.ERROR_MESSAGE);
	            		
	            		if (qualityControlFrame.qcClient != null)
	                    {
	                      try
	                      {
	                    	  qualityControlFrame.qcClient.close();
	                      }
	                      catch (Exception ignore)
	                      {}
	                     }
	            		
	            		System.exit(0);
	            	}
	            	else
	            	{
	            		// empty List
	            		qualityControlFrame.setVisible(true);  
	            	}
	            	
	            }
	            	
	            	
	           
	            }
	        });
	    }

/**
 * Initializes all GUI panels
 */
	    private static ArrayList<Object> initializeAllFields()
	    {
	         ArrayList<Object> message = new ArrayList<Object>();
	        message.add("initializeAllFieldsQualityControl");
	         
	        try 
	        {
	        	
	        	ArrayList<Object> response = qcClient.call(message);
	            return  response; 
	        } catch (Exception ex) {
	            Logger.getLogger(EngravingFrame.class.getName()).log(Level.SEVERE, null, ex);
	        }
	        
	       ArrayList<Object> failed = new ArrayList<Object>();
	       failed.add("initializeAllFieldsQualityControl");
	       failed.add("Failed in initializeAllFieldsQualityControl");
	        
	          return failed;
	    }


}



